package com.shyroke.daydayzhuansvc.service.impl;

import com.shyroke.daydayzhuanapi.constant.RedisKeyPrefix;
import com.shyroke.daydayzhuanapi.entity.FriendsEntity;
import com.shyroke.daydayzhuandao.respository.FriendsRepository;
import com.shyroke.daydayzhuansvc.service.IFriendService;
import com.shyroke.daydayzhuansvc.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author youhj
 * @date 2019/7/30 0030 下午 5:34
 */
@Service
public class FriendService implements IFriendService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FriendsRepository friendsRepository;

    @Override
    public List<FriendsEntity> getList() {

        List<FriendsEntity> friendSiteList = RedisUtils.readList(RedisKeyPrefix.FRIENDS);
        if(CollectionUtils.isEmpty(friendSiteList)){
            Iterator<FriendsEntity> iterator = friendsRepository.findAll().iterator();
            friendSiteList = new ArrayList<>();
            while (iterator.hasNext()){
                FriendsEntity entity = iterator.next();
                friendSiteList.add(entity);
            }
            redisTemplate.opsForValue().set(RedisKeyPrefix.FRIENDS,friendSiteList);
        }

        return friendSiteList;
    }
}
